package org.bapoint.reactadmin.config;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Aspect
@Component
public class LoggingAspect {

    private static final Logger logger = LoggerFactory.getLogger(LoggingAspect.class);

    @Around("execution(* org.bapoint.reactadmin.controller.*.*(..))")
    public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
        // 在方法执行前的操作
        logger.info("Entering method: {}", joinPoint.getSignature().getName());
        logger.info("方法开始时间:"+ LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS")));
        long start = System.currentTimeMillis();
        // 执行原方法
        Object result = joinPoint.proceed();
        long end = System.currentTimeMillis();

        // 在方法执行后的操作
        logger.info("Exiting method: {}. Result: {}", joinPoint.getSignature().getName(), result);
        logger.info("方法结束时间:"+ LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS")));
        logger.info("方法耗时:"+(end-start));
        return result;
    }
}